package com.novax.ex.activity.provider.service;

import com.alibaba.fastjson.JSON;
import com.novax.ex.activity.infrastructure.entity.BeebActivityB;
import com.novax.ex.activity.infrastructure.entity.BeebUser;
import com.novax.ex.activity.infrastructure.mapper.BeebActivityBMapper;
import com.novax.ex.activity.provider.api.model.BeebInternalOrderResponse;
import com.novax.ex.common.base.BaseMapper;
import com.novax.ex.common.base.BaseService;
import com.novax.ex.common.results.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Description
 * @Author zhaolei
 * @Date 11/5/24 11:31 AM
 * @Version 1.0
 */
@Service
@Slf4j
public class BeebActivityBService extends BaseService {
    @Resource
    private BeebActivityBMapper beebActivityBMapper;
    @Resource
    private BeebUserService beebUserService;

    static Long beebUserId = 1001l;//由项目方提供的用户id
    static BigDecimal transferAmount = BigDecimal.TEN;//最低需要转入的金额
    static Integer batchSize = new BigDecimal("1000").divide(new BigDecimal("3"), 0, BigDecimal.ROUND_DOWN).intValue();//总批次


    @Override
    protected BaseMapper getBaseMapper() {
        return beebActivityBMapper;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public void exec(BeebInternalOrderResponse entity) {
        if (check(entity)) {
            //说明是需要进行处理的
            //查询数据
            List<BeebActivityB> beebUsers = beebActivityBMapper.queryByUserId(entity.getFromUserId());
            //只允许一次空投，所以查到数据，就直接不管了
            if (CollectionUtils.isEmpty(beebUsers)) {
                BigDecimal total = entity.getAmount().multiply(new BigDecimal("10"));
                //插入beeb_user
                BeebActivityB user = new BeebActivityB();
                /**
                 * 用户id
                 */
                user.setUserId(entity.getFromUserId());

                /**
                 * 空投总量
                 */
                user.setTotal(total);

                /**
                 * 已空投总量
                 */
                user.setSendTotal(total);

                /**
                 * 是否已空投 0:未空投 1:已空投
                 */
                user.setSendStatus(1);
                /**
                 * 每批次解锁量
                 */
                user.setUnlockBatchTotal(total.divide(new BigDecimal(batchSize + ""), 8, BigDecimal.ROUND_DOWN));

                /**
                 * 总批次
                 */
                user.setUnlockBatch(batchSize);

                /**
                 * 0:不符合解锁条件 1:解锁中 2:解锁完毕
                 */
                user.setStatus(1);
                beebActivityBMapper.insertSelective(user);
                //加资产放最后，防止出错后，调用了加资产导致给用户重复加
                ReturnResult result = beebUserService.addAsset(entity.getFromUserId(), entity.getId(), "beeb", total);
                if (!result.isSuccess()) {
                    log.error("增加此账户的beeb冻结资产失败:" + JSON.toJSONString(entity));
                }
            }
        }
        //在调用方案b的
    }

    //条件检查过滤
    private boolean check(BeebInternalOrderResponse entity) {
        if (entity.getUserId().equals(beebUserId)
                && entity.getCurrency().equals("usdt")
                && entity.getType().equals(1)
                && entity.getStatus().equals(2)
                && entity.getAmount().compareTo(transferAmount) > -1
        ) {
            return true;
        }
        return false;
    }
}
